package visual;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;

import data.MatrixTime;


public class GraphPlotter {
	
	private static final DecimalFormat D_FORMATTER = new DecimalFormat("0.00");
	static {
		D_FORMATTER.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
	}
	
	
	public static String plot(List<MatrixTime> erlang_times, int i, int j) {
		//get description
		StringBuffer sbuf = new StringBuffer();
		
		/*
		List<Double> values = new ArrayList<Double>();
		Calendar start = erlang_times.get(0).start;
		double e_val = 0;
		int cont = 0;
		for(ErlangTime et: erlang_times) {
			if(et.start.get(Calendar.HOUR_OF_DAY) == start.get(Calendar.HOUR_OF_DAY)) {
				e_val = e_val + et.mat[i][j];
				cont ++;
			}
			else {
				values.add(e_val/cont);
				start = et.start;
				e_val = et.mat[i][j];
				cont = 1;
			}
		}
		for(Double v: values) 
			sbuf.append(","+D_FORMATTER.format(v));
		*/
		
		for(MatrixTime et: erlang_times) 
			sbuf.append(","+D_FORMATTER.format(et.getMat()[i][j]));
		
		String s_trend = sbuf.substring(1); 
		
		
		// compute the global mean 
		StringBuffer sbuf2 = new StringBuffer();
		for(MatrixTime et: erlang_times) {
			double mean = 0;
			double tot = 0;
			for(int ii=0; ii<et.getMat().length;ii++)
			for(int jj=0; jj<et.getMat()[ii].length;jj++) {
				if(et.getMat()[ii][jj] > 0) {
					mean = mean + et.getMat()[ii][jj];
					tot ++;
				}
			}
			if(tot > 0)
				mean = mean / tot;
			sbuf2.append(","+D_FORMATTER.format(mean));
		}
		String s_mean = sbuf2.substring(1); 
		
		
		String l_values = s_trend+"|"+s_mean;
		String l_color = "990066,000000";
		String l_size = "3|2";
		
		
		return "<img src=\"http://chart.apis.google.com/chart?chxr=0,-1,1|1,0,23&chxt=y,x&chs=300x225&cht=lc&chco="+l_color+"&chds=-1,1&chd=t:"+l_values+"&chg=14.3,-1,1,1&chls="+l_size+"&chm=B,C5D4B5BB,0,0,0\" width=\"300\" height=\"225\" alt=\"\" />";
	}
	
	public static String plot(List<MatrixTime> erlang_times, List<MatrixTime> mean_times, int i, int j) {
		//get description
		StringBuffer sbuf = new StringBuffer();
		
		for(MatrixTime et: erlang_times) 
			sbuf.append(","+D_FORMATTER.format(et.getMat()[i][j]));
		
		String s_trend = sbuf.substring(1); 
		
		
		// compute the mean 
		StringBuffer sbuf2 = new StringBuffer();
		for(MatrixTime et: mean_times) 
			sbuf2.append(","+D_FORMATTER.format(et.getMat()[i][j]));
		
		String s_mean = sbuf2.substring(1);
		
		String l_values = s_trend+"|"+s_mean;
		String l_color = "990066,000000";
		String l_size = "3|2";
		
		
		return "<img src=\"http://chart.apis.google.com/chart?chxr=0,-1,1|1,0,23&chxt=y,x&chs=300x225&cht=lc&chco="+l_color+"&chds=-1,1&chd=t:"+l_values+"&chg=14.3,-1,1,1&chls="+l_size+"&chm=B,C5D4B5BB,0,0,0\" width=\"300\" height=\"225\" alt=\"\" />";
	}
}
